package com.zyn.inventory.management.dao;

import com.zyn.inventory.management.entities.QUser;
import com.zyn.inventory.management.entities.User;
import com.zyn.inventory.management.utils.Page;

import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDetailsDaoImpl extends BaseDaoImpl<User> implements
		UserDetailsDao {
	QUser userDetails = QUser.user;

	public List<User> getAll() {
		return query().from(userDetails).listDistinct(userDetails);
	}

	public List<User> get(List<Long> ids) {
		if (ids == null || ids.isEmpty()) {
			return new ArrayList<User>();
		}
		return query().from(userDetails).where(userDetails.id.in(ids))
				.list(userDetails);
	}

	public User findByEmail(String email) {
		return query().from(userDetails)
				.where(userDetails.email.equalsIgnoreCase(email))
				.uniqueResult(userDetails);
	}

	public User findByUserName(String userName) {
		return query().from(userDetails)
				.where(userDetails.username.equalsIgnoreCase(userName))
				.uniqueResult(userDetails);
	}

	public List<User> getBatch(List<Long> ids) {
		return query().from(userDetails).where(userDetails.id.in(ids))
				.listDistinct(userDetails);
	}

	public long countAll() {
		return query().from(userDetails).count();
	}

	public List<User> searchByEmail(String email) {
		return query().from(userDetails)
				.where(userDetails.email.like("%" + email + "%"))
				.list(userDetails);
	}

	public Page<User> queryPage(Integer page) {
		Page<User> pageModel = new Page<User>(this.countAll(), page);
		List<User> users = query().from(userDetails).offset((pageModel.getPageNumber()-1)*pageModel.getLimit()).limit(pageModel.getLimit()).list(userDetails);
		pageModel.setList(users);
		return pageModel;
	}
	

}
